package br.ufal.tci.nexos.arcolive;

import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotAddServiceInstanceException;

public class ArCoLIVEServiceInstanceGroup {
	private ArCoLIVEServiceInstance[] serviceInstances;
	private int count = 0;
	
	public ArCoLIVEServiceInstanceGroup(int maxInstances) {
		this.serviceInstances = new ArCoLIVEServiceInstance[maxInstances];
	}
	
	public void addServiceInstance(ArCoLIVEServiceInstance serviceInstance) throws ArCoLIVECannotAddServiceInstanceException {
		if (count+1 <= this.serviceInstances.length) {
			this.serviceInstances[this.count++] = serviceInstance;
		} else {
			throw new ArCoLIVECannotAddServiceInstanceException("Max instance number reached");
		}
	}
	
	public ArCoLIVEServiceInstance getServiceInstance(int index) {
		return this.serviceInstances[index];
	}
	
	public ArCoLIVEServiceInstance getServiceInstanceById(int serviceInstanceId) {
		int size = this.serviceInstances.length;
		for (int i = 0; i < size; i++) {
			if (this.serviceInstances[i].getServiceInstanceDescriptor().getServiceInstanceId() == serviceInstanceId) {
				return this.serviceInstances[i];
			}
		}
		return null;
	}

	public int getCount() {
		return count;
	}

	public ArCoLIVEServiceInstance[] getServiceInstances() {
		ArCoLIVEServiceInstance[] result = new ArCoLIVEServiceInstance[this.count];
		for (int i = 0; i < this.count; i++) {
			result[i] = this.serviceInstances[i];
		}
		return result;
	}
	
}
